#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
icon生成
'''

import sys
reload(sys)
sys.setdefaultencoding('UTF8')
import os

import json
from PIL import Image

import progressbar
import argparse

tpl = '{"images":[{"idiom":"iphone","size":"20x20","scale":"2x"},{"idiom":"iphone","size":"20x20","scale":"3x"},{"idiom":"iphone","size":"29x29","scale":"2x"},{"idiom":"iphone","size":"29x29","scale":"3x"},{"idiom":"iphone","size":"40x40","scale":"2x"},{"idiom":"iphone","size":"40x40","scale":"3x"},{"idiom":"iphone","size":"60x60","scale":"2x"},{"idiom":"iphone","size":"60x60","scale":"3x"},{"idiom":"ipad","size":"20x20","scale":"1x"},{"idiom":"ipad","size":"20x20","scale":"2x"},{"idiom":"ipad","size":"29x29","scale":"1x"},{"idiom":"ipad","size":"29x29","scale":"2x"},{"idiom":"ipad","size":"40x40","scale":"1x"},{"idiom":"ipad","size":"40x40","scale":"2x"},{"idiom":"ipad","size":"76x76","scale":"1x"},{"idiom":"ipad","size":"76x76","scale":"2x"},{"idiom":"ipad","size":"83.5x83.5","scale":"2x"},{"idiom":"ios-marketing","size":"1024x1024","scale":"1x"},{"idiom":"car","size":"60x60","scale":"2x"},{"idiom":"car","size":"60x60","scale":"3x"},{"size":"24x24","idiom":"watch","scale":"2x","role":"notificationCenter","subtype":"38mm"},{"size":"27.5x27.5","idiom":"watch","scale":"2x","role":"notificationCenter","subtype":"42mm"},{"size":"29x29","idiom":"watch","role":"companionSettings","scale":"2x"},{"size":"29x29","idiom":"watch","role":"companionSettings","scale":"3x"},{"size":"40x40","idiom":"watch","scale":"2x","role":"appLauncher","subtype":"38mm"},{"size":"44x44","idiom":"watch","scale":"2x","role":"appLauncher","subtype":"40mm"},{"size":"50x50","idiom":"watch","scale":"2x","role":"appLauncher","subtype":"44mm"},{"size":"86x86","idiom":"watch","scale":"2x","role":"quickLook","subtype":"38mm"},{"size":"98x98","idiom":"watch","scale":"2x","role":"quickLook","subtype":"42mm"},{"size":"108x108","idiom":"watch","scale":"2x","role":"quickLook","subtype":"44mm"},{"idiom":"watch-marketing","size":"1024x1024","scale":"1x"},{"idiom":"mac","size":"16x16","scale":"1x"},{"idiom":"mac","size":"16x16","scale":"2x"},{"idiom":"mac","size":"32x32","scale":"1x"},{"idiom":"mac","size":"32x32","scale":"2x"},{"idiom":"mac","size":"128x128","scale":"1x"},{"idiom":"mac","size":"128x128","scale":"2x"},{"idiom":"mac","size":"256x256","scale":"1x"},{"idiom":"mac","size":"256x256","scale":"2x"},{"idiom":"mac","size":"512x512","scale":"1x"},{"idiom":"mac","size":"512x512","scale":"2x"}],"info":{"version":1,"author":"xcode"}}'

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Create iPhone iPad Mac AppleWatch CarPlay Icon')

    parser.add_argument('image', type=str, help='Set Image Path')
    parser.add_argument('-t', '--type', type=str, help='Set values (iphone/ipad/mac/watch/car)', default='iphone/ipad')
    parser.add_argument('-n', '--name', type=str, help='Set Icon Image Name Prefix (Default Icon-App)', default='Icon-App')

    args = parser.parse_args()
    typeList = [x.lower() for x in args.type.split('/')]

    if 'iphone' in typeList or 'ipad' in typeList:
        typeList.append('ios-marketing')

    if 'watch' in typeList:
        typeList.append('watch-marketing')

    if not os.path.isfile(args.image):
        print 'No Image File'
        sys.exit(0)

    try:
        im = Image.open(args.image)
        im.verify()
    except:
        print 'Not Image File'
        sys.exit(0)

    fileExt = args.image.split('.')[-1]

    if not os.path.isdir('./AppIcon.appiconset'):
        os.mkdir('./AppIcon.appiconset')

    tplDict = json.loads(tpl)
    outputDict = {'images':[], 'info':tplDict['info']}

    pbar = progressbar.ProgressBar(len(tplDict['images']))
    pbar.start()

    for item in tplDict['images']:
        if item['idiom'] in typeList:
            scale = float(item['scale'].replace('x', ''))
            width, height = [float(x) * scale for x in item['size'].split('x')]
            iconFileName = args.name + '-' + item['idiom'] + '-' + item['size'] + '@' + item['scale']

            #print item['size'], item['scale'], './AppIcon.appiconset/' + '/' + iconFileName + '.' + fileExt
            im = Image.open(args.image)
            im.resize((int(width), int(height)), Image.ANTIALIAS).save('./AppIcon.appiconset/' + '/' + iconFileName + '.' + fileExt)
            im.close()
            item['filename'] = iconFileName + '.' + fileExt

            outputDict['images'].append(item)
            pbar.update(len(outputDict['images']))

    contents = json.dumps(outputDict, sort_keys=True, indent=2, separators=(',', ' : '))

    fh = open('./AppIcon.appiconset/Contents.json', 'w')
    fh.write(contents)
    fh.close()

    pbar.finish()



